package com.vlin.device.value.converter.condition.utils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.vlin.device.value.converter.condition.TuyaCondition;
import com.vlin.device.value.converter.condition.TuyaEnum;
import com.vlin.device.value.converter.condition.TuyaEnumCondition;
import com.vlin.device.value.converter.condition.TuyaInteger;
import com.vlin.device.value.converter.condition.TuyaIntegerCondition;

public class TuyaUtils {

	private static TuyaCondition BOOLEAN_TUYA_CONDITION = new TuyaCondition(Boolean.class);
	
	private static final Map<TuyaInteger, TuyaIntegerCondition> tuyaIntegerConditionMap = new ConcurrentHashMap<>();
	
	private static final Map<TuyaEnum, TuyaEnumCondition> tuyaEnumConditionMap = new ConcurrentHashMap<>();
	
	public static TuyaCondition getBooleanCondition() {
		return BOOLEAN_TUYA_CONDITION;
	}
	
	public static int stringHash(String value) {
		int result = 0;
		if(value == null)
			return result;
		char[] cs = value.toCharArray();
		for(int i = 0;i <cs.length; i ++) {
			char c = cs[i];
			result = result + c * (i+1);
		}
		return result;
	}
	
	public static TuyaCondition getIntegerCondition(TuyaInteger tuyaInteger) {
		
		if(tuyaInteger == null) {
			throw new NullPointerException("TuyaInteger is null");
		}
		
		if(tuyaIntegerConditionMap.containsKey(tuyaInteger)) {
			return tuyaIntegerConditionMap.get(tuyaInteger);
		}
		TuyaIntegerCondition condition = new TuyaIntegerCondition(tuyaInteger);
		
		tuyaIntegerConditionMap.put(tuyaInteger, condition);
		return condition;
	}
	
	public static TuyaCondition getEnumCondition(TuyaEnum tuyaEnum) {
		if(tuyaEnum == null) {
			throw new NullPointerException("TuyaEnum is null");
		}
		
		if(tuyaEnumConditionMap.containsKey(tuyaEnum)) {
			return tuyaEnumConditionMap.get(tuyaEnum);
		}
		TuyaEnumCondition condition = new TuyaEnumCondition(tuyaEnum);
		
		tuyaEnumConditionMap.put(tuyaEnum, condition);
		return condition;
	}
	

}
